package com.demo.modules.mq.redis.service;

import java.util.concurrent.TimeUnit;

import com.demo.common.redis.service.RedisService;
import com.demo.modules.mq.redis.RedisAnnouncementMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 消息消费者
 *
 * @author Rog
 */
@Service
public class RedisMessageConsumerService extends Thread {


    @Autowired
    private RedisService redisService;

    private volatile boolean flag = true;

    private String queueKey = "queue";

    private Long popTime = 1000L;

    @Override
    public void run() {

        RedisAnnouncementMessage message;
        // 为了能一直循环而不结束
        while (flag && !Thread.currentThread().isInterrupted()) {
            try {
                message = (RedisAnnouncementMessage) redisService.redisTemplate.opsForList().rightPop(queueKey, popTime, TimeUnit.SECONDS);
                Thread.sleep(1000);
                System.out.println("接收到了" + message);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

}
